﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Coolite.Ext.Web;
using System.Collections.Generic;

namespace Sagacity.NC.Web
{
    public partial class ModuleMenus : PageBase
    {
        Business.SysMange.Common comm = new Sagacity.NC.Business.SysMange.Common();

        protected void Page_Load(object sender, EventArgs e)
        {
            string userName = CurrentUser.UserName;
            if (!IsPostBack)
            {
                int moduleid = Request.QueryString["sysModuleId"] == null ? 0 : int.Parse(Request.QueryString["sysModuleId"]);
                Sagacity.Entity.Build.ModuleItemInfo moduleInfo = comm.GetModuleMapping(moduleid);
                if (moduleInfo != null)
                    CreateMenu(CurrentUser.LoginRole.Id, moduleInfo.Id);
            }
        }

        private void CreateMenu(int rid, int moduleId)
        {
            List<Sagacity.Entity.Build.ModuleItemInfo> menulist = comm.GetMenuList(rid, moduleId);

            for (int i = 0; i < menulist.Count; i++)
            {
                Coolite.Ext.Web.Panel panelMenu;
                //父菜单
                panelMenu = new Coolite.Ext.Web.Panel(menulist[i].Caption);
                panelMenu.ID = menulist[i].Id.ToString();
                panelMenu.IconCls = "icon-function-menu";
                panelMenu.BodyStyle = "background-color: #FFFFFF; overflow-x:hidden;SCROLLBAR-FACE-COLOR: #e8e7e7; SCROLLBAR-HIGHLIGHT-COLOR: #ffffff; SCROLLBAR-SHADOW-COLOR: #ffffff; SCROLLBAR-3DLIGHT-COLOR: #cccccc; SCROLLBAR-ARROW-COLOR: #03B7EC; SCROLLBAR-TRACK-COLOR: #EFEFEF; SCROLLBAR-DARKSHADOW-COLOR: #b2b2b2; SCROLLBAR-BASE-COLOR: #000000;";
                panelMenu.AutoScroll = true;
                try
                {
                    if (!string.IsNullOrEmpty(menulist[i].ImageUrl))
                        panelMenu.Icon = (Icon)Enum.Parse(typeof(Icon), menulist[i].ImageUrl);
                }
                catch { }

                //总表格,包含描述以及子菜单
                System.Web.UI.HtmlControls.HtmlTable tParent = new System.Web.UI.HtmlControls.HtmlTable();

                //描述行
                System.Web.UI.HtmlControls.HtmlTableRow rDes = new System.Web.UI.HtmlControls.HtmlTableRow();
                System.Web.UI.HtmlControls.HtmlTableCell cDes = new System.Web.UI.HtmlControls.HtmlTableCell();
                cDes.Align = "left";

                Coolite.Ext.Web.FieldSet fs = new FieldSet();
                fs.CheckboxToggle = true;

                Coolite.Ext.Web.Panel panelDes = new Coolite.Ext.Web.Panel();
                panelDes.BodyStyle = "background-color: #FFFFFF;";
                Coolite.Ext.Web.Label lblDes = new Coolite.Ext.Web.Label();

                string strDes = menulist[i].Comments;
                lblDes.Html = "<span style='font-family: 宋体;font-size: 12px;color: #666666;line-height:18px;'>" + strDes + "</span>";
                fs.BodyControls.Add(lblDes);
                //如果描述为空，则不显示fs控件
                //fs.Visible = !string.IsNullOrEmpty(strDes);

                cDes.Controls.Add(fs);
                rDes.Controls.Add(cDes);
                tParent.Controls.Add(rDes);


                //子菜单
                List<Sagacity.Entity.Build.ModuleItemInfo> childlist = comm.GetMenuList(rid, menulist[i].Id);
                if (childlist.Count == 0)
                {
                    //不存在子菜单,增加描述后进入下一层
                    panelMenu.BodyControls.Add(tParent);
                    Accordion1.Items.Add(panelMenu);
                    continue;
                }

                for (int j = 0; j < childlist.Count; j++)
                {
                    System.Web.UI.HtmlControls.HtmlTableRow rChildMenu = new System.Web.UI.HtmlControls.HtmlTableRow();
                    System.Web.UI.HtmlControls.HtmlTableCell cChildMenu = new System.Web.UI.HtmlControls.HtmlTableCell();

                    System.Web.UI.HtmlControls.HtmlTable t = new System.Web.UI.HtmlControls.HtmlTable();
                    t.Border = 0;
                    t.CellPadding = 0;
                    t.CellSpacing = 0;
                    t.Width = "140px";
                    t.Attributes.Add("style", "margin-top:9px; display:inline;margin-left:23px; color:#000000; ");


                    System.Web.UI.HtmlControls.HtmlTableRow r = new System.Web.UI.HtmlControls.HtmlTableRow();
                    System.Web.UI.HtmlControls.HtmlTableCell c = new System.Web.UI.HtmlControls.HtmlTableCell();
                    c.Align = "right";
                    c.VAlign = "center";

                    System.Web.UI.HtmlControls.HtmlTable tChild = new System.Web.UI.HtmlControls.HtmlTable();
                    tChild.Width = "140";
                    tChild.Height = "18";
                    tChild.Border = 0;
                    tChild.CellSpacing = 0;
                    tChild.CellPadding = 0;
                    System.Web.UI.HtmlControls.HtmlTableRow rChild = new System.Web.UI.HtmlControls.HtmlTableRow();
                    System.Web.UI.HtmlControls.HtmlTableCell cChild = new System.Web.UI.HtmlControls.HtmlTableCell();
                    cChild.Width = "25";
                    cChild.Align = "left";
                    cChild.VAlign = "center";
                    cChild.Attributes.Add("class", "LoginInfoBlackText");
                    System.Web.UI.HtmlControls.HtmlImage imgChild = new System.Web.UI.HtmlControls.HtmlImage();
                    imgChild.Src = "resources/images/gt_left_Ico_on.gif";
                    imgChild.Width = 14;
                    imgChild.Height = 14;

                    System.Web.UI.HtmlControls.HtmlTableCell cChild1 = new System.Web.UI.HtmlControls.HtmlTableCell();
                    cChild1.Width = "115";
                    cChild1.Align = "left";
                    cChild1.VAlign = "bottom";
                    cChild1.Attributes.Add("style", "color:#000000; font-size:12px;line-height:25px;");

                    Coolite.Ext.Web.LinkButton btn = new Coolite.Ext.Web.LinkButton();
                    btn.Text = childlist[j].Caption;
                    btn.Flat = true;
                    btn.Attributes.Add("style", "text-decoration:underline;");

                    btn.Listeners.Click.Handler = "Coolite.AjaxMethods.AddTabs(" + childlist[j].Id.ToString() + ",{success: function (result) {self.parent.addTabs(self.parent.TabPanel1,result);}});";

                    t.Rows.Add(r);
                    r.Cells.Add(c);
                    c.Controls.Add(tChild);

                    tChild.Rows.Add(rChild);
                    rChild.Cells.Add(cChild);
                    rChild.Controls.Add(cChild1);
                    cChild.Controls.Add(imgChild);
                    cChild1.Controls.Add(btn);

                    tParent.Controls.Add(rChildMenu);
                    rChildMenu.Controls.Add(cChildMenu);
                    cChildMenu.Controls.Add(t);

                }
                panelMenu.BodyControls.Add(tParent);
                Accordion1.Items.Add(panelMenu);
            }
        }

        [AjaxMethod]
        public string AddTabs(string nodeId)
        {
            string rootTabs= comm.GetTabs(comm.GetRoleListByUserName(CurrentUser.UserName)[0].Id, int.Parse(nodeId));
            return rootTabs.Replace("@roleID", CurrentUser.LoginRole.Id.ToString());
        }
    }
}
